<?php

/**
 * Description of Helper_Mail
 * Helper mail es un helper de envio de correo electronico, si requieres una correo en especial
 * configura un metodo.
 *
 * @author us
 */
class Helper_MailHelper
{
    /* Cambiar el banner cuando se tenga uno mejor y su ruta, y la direccion de correo de la firma */

    private $_banner = '<div style="padding: 14px; margin-bottom: 4px; background-color: rgb(0, 0, 0); -moz-border-radius: 5px 5px 5px 5px;">
                            <a target="_blank" href="http://www.dealbussinesscenter.com" style="color: rgb(0, 0, 0);">
                                <img width="186" height="83" style="display: block; border: 0pt none;"
                                    src="http://www.logistic-apps.com/dev/css/smart/images/logo.png" alt="DealBussinessCenter">
                            </a>
                           </div>';
    private $_title_start = '<h2 style="margin: 0pt 0pt 16px; font-size: 18px; font-weight: normal;">';
    private $_title_end = '</h2>';
    private $_title = '';
    private $_body_start = '<div style="background-color:white;"><p style="  color: #000000; font-family: Helvetica,Arial,sans-serif; font-size: 12px;">';
    private $_body_end = '</p></div>';
    private $_body_html = '';
    private $_body_alt = '';
    private $_firma_HTML = '<span style="font: 13px Georgia; color: rgb(102, 102, 102);">Deal Bussiness Center</span><hr>
                            <div style="text-align: center; color: rgb(102, 102, 102);">
                            Please do not reply to this email; replies are not monitored.<br/>
                            Deal Business Center by Logistic Apps<br/>
                            Manuel José Othón 423 Centro S.L.P. México<br/>
                            </div>';
    private $_firma_ALT = 'Deal Business Center by Logistic Apps | Manuel Jose Othon 423 Centro S.L.P. Mexico';
    private $_mail = null;

    function __construct()
    {
        if ($this->_mail == null)
            $this->_mail = new Mailer_Sender();  // solo cuando es null la instanciamos
    }

    /**
     * Metodo privado que crea, y prepara las situaciones para envio de mail.
     * @param string $destiny
     * @return mixed false: error al crear mail sender , Mailer_Sender
     */
    private function preparedSend($fromName = 'DealBussinessCenter')
    {
        //instanciamos un objeto de la clase phpmailer al que llamamos
        //por ejemplo mail

        $mail = $this->_mail;

        $mail->IsSMTP();

        $mail->Mailer = "smtp";
        $mail->Host = 'ssl://email-smtp.us-east-1.amazonaws.com';
        $mail->Port = 465;
        $mail->SMTPAuth = true;        
        $mail->Username = 'AKIAIU6WHO4LZ4UVK5GQ';
        $mail->Password = 'AskCSFszKBX3B+xA75olDrYt9TnCTUlPa269AkiDuMos';
        $mail->From = 'noreply@dealbusinesscenter.com';
        $mail->FromName = $fromName;

//        if($mail->Host=='smtp.gmail.com' || $host=='smtp.live.com')
//        {
//            $mail->SMTPSecure = 'tls';
//        }
        return $mail;
    }
private function setMail($mail, $subject)
	    {
	        $msjHTML = '';

	        $msjHTML .= $this->_title_start . $this->_title . $this->_title_end . '<br><br>';
	        $msjHTML .= $this->_body_start . $this->_body_html . $this->_body_end . '<br>';
	        $msjHTML .= $this->_firma_HTML;
        //Asignamos asunto y cuerpo del mensaje
	        //El cuerpo del mensaje lo ponemos en formato html, haciendo
	        //que se vea en negrita
        $subject = utf8_decode($subject);
	        $mail->Subject = $subject;
        $msjAlt = $this->_body_alt . $this->_firma_ALT;

	        $mail->AltBody = $msjAlt;
	        $mail->Body = $msjHTML;
	        $pattern = array(
	            '/Ã¡/' => '&aacute;',
	            '/Ã©/' => '&eacute;',
            '/Ã­/' => '&iacute;',
	            '/Ã³/' => '&oacute;',
	            '/Ãº/' => '&uacute;',
	            '/Ã/' => '&Aacute;',
	            '/Ã/' => '&Eacute;',
	            '/Ã/' => '&Iacute;',
	            '/Ã/' => '&Oacute;',
	            '/Ã/' => '&Uacute;',
	            '/\n/' => '<br>'
	        );
	        //$mail->Body = $this->_banner . preg_replace(array_keys($pattern), array_values($pattern), $mail->Body);
	        $mail->Body = preg_replace(array_keys($pattern), array_values($pattern), $mail->Body);
	        $mail->IsHTML(true);
	
	        //Se limpian las variables
	        $this->_title = '';
	        $this->_body_alt = '';
	        $this->_body_html = '';
	
	        return $mail;
    }
	
    
    /**
     * Metodo que envia el correo electronico  n numero de veces,
     * @param Mailer_Sender $mail
     * @param int $numTrials
     * @return boolean
     */
    private function sendMail($mail, $numTrials=4)
    {
        //se envia el mensaje, si no ha habido problemas
        //la variable $exito tendra el valor true
        $exito = $mail->Send();
       // var_dump($exito);
        //Si el mensaje no ha podido ser enviado se realizaran 4 intentos mas como mucho
        //para intentar enviar el mensaje, cada intento se hara 5 segundos despues
        //del anterior, para ello se usa la funcion sleep
        $intentos = 1;
        while ((!$exito) && ($intentos < $numTrials))
        {
            sleep(5);
           // $exito = $mail->Send();
            $exito = true;

            $intentos = $intentos + 1;
        }
        $mail->ClearAddresses();
        return $exito;
    }

    /**
     * Metodo que envia un correo al enviar una invitacion
     * @param <type> $destiny
     * @param <type> $nameSender
     * @param <type> $message
     * @param <type> $date
     * @param <type> $numTrials
     * @return <type>
     */
    public function sendInvitation($destiny, $userSender, $message, $date, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny->getUsername());
        $subject = 'Invitation to '.$destiny->getFirstName().' '.$destiny->getLastName().' at AWC2012';
        $this->_title = 'You have an invitation: <br/>';
        $this->_body_html .= '<strong>Dear '.$destiny->getFirstName().' '.$destiny->getLastName().'</strong>,<br/>
         You are receiving this urgent email because you are a registered attendee of AWC 2012 and because another attendee would like to meet with you at the show.
         <br/>'.$userSender->getFirstName().' '.$userSender->getLastName().' of '.$userSender->getCompany().', would like to meet you on '.$date->format('Y-m-d  H:i').' at AWC 2012.<br/>
        ';
        $this->_body_html .= '<strong>Attached Message: </strong> <br/>'.$message . '<br/>';
        //$this->_body_alt .= 'Name: '.$userSender->getFirstName().' '.$userSender->getLastName(). PHP_EOL;
        $this->_body_html .= '<br/>If you have not yet used the Deal Business Center, please register once you visit the link below. The registration will take less than two minutes of your time, and will give you immediate access to other attendee information, so you can request additional meetings.
        <br/>Please confirm or decline this meeting by clicking <a href="http://awc2012.dealbusinesscenter.com">here</a>';
        //$this->_body_alt .= 'Company: '.$userSender->getCompany().PHP_EOL;        
       // $this->_body_alt .=  'Date meeting: ' . $date->format('Y-m-d  H:i') . PHP_EOL;        
        //$this->_body_alt .= 'Message:' . PHP_EOL . $message . PHP_EOL;
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }
    
    /**
     * Metodo que envia un correo al enviar un mensaje
     * @param <type> $destiny
     * @param <type> $nameSender
     * @param <type> $message
     * @param <type> $date
     * @param <type> $numTrials
     * @return <type>
     */
    public function sendMessage($destiny, $userSender, $message, $date, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny->getUsername());
        $subject = 'Message to '.$destiny->getFirstName().' '.$destiny->getLastName().' at AWC2012';
        $this->_title = 'You have an message: <br/>';
        $this->_body_html .= '<strong>Dear '.$destiny->getFirstName().' '.$destiny->getLastName().'</strong>,<br/>
         You are receiving this urgent email because you are a registered attendee of AWC 2012 and because another attendee would like to meet with you at the show.
         <br/>'.$userSender->getFirstName().' '.$userSender->getLastName().' of '.$userSender->getCompany().'<br/>
        ';
        $this->_body_html .= '<strong>Attached Message: </strong> <br/>'.$message . '<br/>';
        //$this->_body_alt .= 'Name: '.$userSender->getFirstName().' '.$userSender->getLastName(). PHP_EOL;
        $this->_body_html .= '<br/>If you have not yet used the Deal Business Center, please register once you visit the link below. The registration will take less than two minutes of your time, and will give you immediate access to other attendee information, so you can request additional meetings.
        <br/>Please confirm or decline this message by clicking <a href="http://awc2012.dealbusinesscenter.com">here</a>';
        //$this->_body_alt .= 'Company: '.$userSender->getCompany().PHP_EOL;        
       // $this->_body_alt .=  'Date meeting: ' . $date->format('Y-m-d  H:i') . PHP_EOL;        
        //$this->_body_alt .= 'Message:' . PHP_EOL . $message . PHP_EOL;
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }

    /**
     * Metodo que envia un correo al responder a una invitacion
     * @param <type> $destiny
     * @param <type> $nameSender
     * @param <type> $message
     * @param <type> $date
     * @param <type> $numTrials
     * @return <type>
     */
    public function sendResponseInvitation($destiny, $userSender, $date, $response, $message, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'Response from ' . $userSender->getFirstName() . ' ' . $userSender->getLastName();
        $status = ($response == 1) ? 'accepted' : 'rejected';
        $this->_title = 'Your request has been ' . $status . ' <br/>';
        $this->_body_html .= $userSender->getFirstName() . ' ' . $userSender->getLastName() . ' of ' . $userSender->getCompany() . ' has ' . $status . ' your meeting request.<br/>';
        $this->_body_alt .= $userSender->getFirstName() . ' ' . $userSender->getLastName() . ' of ' . $userSender->getCompany() . ' has ' . $status . ' your meeting request.' . PHP_EOL;
        $this->_body_html .= '<strong>Meeting requested on </strong>' . $date->format('F j, Y, g:i a') . '<br/><br/>';
        $this->_body_alt .= 'Meeting requested on :' . $date->format('F j, Y, g:i a') . PHP_EOL;
        if (strlen($message) > 0)
        {
            $this->_body_html .= '<strong>Message: </strong> ' . $message . '<br/>';
            $this->_body_alt .= 'Message:' . $message . PHP_EOL;
        }

        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }

    /**
     * Metodo que envia un nuevo password por correo
     * @param <type> $destiny
     * @param <type> $password
     * @param <type> $numTrials
     * @return <type>
     */
    public function sendPassword($destiny, $password, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'Recover Password';
        $this->_title = 'Password';
        $this->_body_html .= '<strong>Email: </strong>' . $destiny . '<br/>';
        $this->_body_alt .= 'Email: ' . $destiny . PHP_EOL;
        $this->_body_html .= '<strong>New Password: </strong>' . $password . '<br/>';
        $this->_body_alt .= 'New Password: ' . $password . PHP_EOL;
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }

    public function sendFeedback($destiny, $userSender, $message, $date, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'New Feedback Deal Bussiness Center';
        $this->_title = 'You have a feedback: <br/>';
        $this->_body_html .= '<strong>Name: </strong>' . $userSender->getFirstName() . ' ' . $userSender->getLastName() . '<br/>';
        $this->_body_alt .= 'Name: ' . $userSender->getFirstName() . ' ' . $userSender->getLastName() . PHP_EOL;
        $this->_body_html .= '<strong>E-mail: </strong>' . $userSender->getUsername() . '<br/>';
        $this->_body_alt .= 'E-mail: ' . $userSender->getUsername() . PHP_EOL;
        $this->_body_html .= '<strong>Company: </strong>' . $userSender->getCompany() . '<br/>';
        $this->_body_alt .= 'Company: ' . $userSender->getCompany() . PHP_EOL;
        $this->_body_html .= '<strong>Date feedback: </strong>' . $date->format('Y-m-d  H:i') . '<br/><br/>';
        $this->_body_alt .= 'Date feedback: ' . $date->format('Y-m-d  H:i') . PHP_EOL;
        $this->_body_html .= '<strong>Feedback: </strong> <br/>' . $message . '<br/>';
        $this->_body_alt .= 'Feedback:' . PHP_EOL . $message . PHP_EOL;
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }
    
    public function sendMailWelcome($destiny,$user, $date,$numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'Welcome to Deal Bussiness Center AWC 2012';
        $this->_title = 'Welcome <strong>'.$user->getFirstName().' '.$user->getLastName().'</strong><br/>';
        $this->_body_html .= 'Your account for Deal Business Center AWC 2012 has been created successfully! <br/>';
        $this->_body_alt .= 'Your account for Deal Business Center AWC 2012 has been created successfully! <br/>';
        $this->_body_html .= 'Deal Business Center Portal will allow you to manage all your profile information and meetings requests.<br/><br/>';
        $this->_body_alt .= 'Deal Business Center Portal will allow you to manage all your profile information and meetings requests.<br/><br/>';
        $this->_body_html .= 'You can log in to Deal Business Center AWC 2012 Portal at:';
        $this->_body_alt .= 'You can log in to Deal Business Center AWC 2012 Portal at:';
        $this->_body_html .=' <a href="http://awc2012.dealbusinesscenter.com">http://awc2012.dealbusinesscenter.com</a>';
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    
    }

    public function sendMailNewRegister($destiny,$user, $date,$numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'Welcome to Deal Bussiness Center AWC 2012';
        $this->_title = 'Welcome <strong>'.$user->getFirstName().' '.$user->getLastName().'</strong><br/>';
        $this->_body_html .= 'Your account for Deal Business Center AWC 2012 has been created successfully! <br/>';
        $this->_body_alt .= 'Your account for Deal Business Center AWC 2012 has been created successfully! <br/>';
        $this->_body_html .= 'Deal Business Center Portal will allow you to manage all your profile information and meetings requests.<br/><br/>';
        $this->_body_alt .= 'Deal Business Center Portal will allow you to manage all your profile information and meetings requests.<br/><br/>';
        $this->_body_html .= 'You can log in to Deal Business Center AWC 2012 Portal at:';
        $this->_body_alt .= 'You can log in to Deal Business Center AWC 2012 Portal at:';
        $this->_body_html .=' <a href="http://awc2012.dealbusinesscenter.com">http://awc2012.dealbusinesscenter.com</a>';
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    
    }
     public function sendMailBusinessComercial($destiny, $userSender, $message, $date, $numTrials=4)
    {
        $mail = $this->preparedSend();
        $mail->AddAddress($destiny);
        $subject = 'Nueva Solicitud De Evento De Deal Bussiness Center Comercial';
        $this->_title = 'Usted tiene una nueva solicitud de evento: <br/>';
        $this->_body_html .= '<strong>Name: </strong>'.$userSender->getFirstName().' '.$userSender->getLastName().'<br/>';
        $this->_body_alt .= 'Name: '.$userSender->getFirstName().' '.$userSender->getLastName(). PHP_EOL;
        $this->_body_html .= '<strong>E-mail: </strong>'.$userSender->getUsername().'<br/>';
        $this->_body_alt .= 'E-mail: '.$userSender->getUsername().PHP_EOL;
        $this->_body_html .= '<strong>Phone: </strong>'.$userSender->getPhone().'<br/>';
        $this->_body_alt .= 'Phone: '.$userSender->getPhone().PHP_EOL;
        $this->_body_html .= '<strong>Date Send: </strong>' . $date->format('Y-m-d  H:i') . '<br/><br/>';
        $this->_body_alt .= 'Date Send: ' . $date->format('Y-m-d  H:i') . PHP_EOL;
        $this->_body_html .= '<strong>Comments: </strong> <br/>'.$message . '<br/>';
        $this->_body_alt .= 'Comments:' . PHP_EOL . $message . PHP_EOL;
        $mail = $this->setMail($mail, $subject);
        $success = $this->sendMail($mail, $numTrials);
        return $success;
    }

}
